package app.jppfnetmorph;

import java.util.ArrayList;
import java.util.List;

import neuron.NetworkStatisticsProxy;

import org.jppf.client.*;
import org.jppf.server.protocol.JPPFTask;

public class JPPFRunner
{
	private static JPPFClient jppfClient =  null;
	
	static {
		jppfClient = new JPPFClient();
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				JPPFRunner.close();
			}
		});
	}

	static void close()
	{
		if (jppfClient != null) jppfClient.close();		
	}

	
	public static List<JobResult> runJob(JPPFJob job) throws Exception
	{
		job.setBlocking(true);

		List<JPPFTask> results = jppfClient.submit(job);
		List<JobResult> ret = new ArrayList<JobResult>();
		
		// process the results
		for (JPPFTask task: results)
		{
			// if the task execution resulted in an exception
			if (task.getException() != null) {
				System.out.println("*** EXCEPTION ***" + task.getException());
				task.getException().printStackTrace();
			}
			else {
				//System.out.println("*** RESULT *** " + task.getResult());
				ret.add((JobResult)task.getResult());
			}
		}
		
		return ret;
	}

}
